package com.powernode.util;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.powernode.common.BaseEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2022/3/24 20:51
 * @Version 1.0
 */
public class Generator {
    public static void main(String[] args) {
        //获取项目路径
        String parent = System.getProperty("user.dir");
        //自定义entity路径
        String entityPath = parent + "/entity/src/main/java/com/powernode/entity";
        //定义其他项目路径
        String otherPath = "/admin";
        //定义生成的表名
        String tableName = "ums_role_resource";
        //创建生成器对象
        AutoGenerator mpg = new AutoGenerator();

        //-------配置开始--------
        //定义全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOpen(false);//生成完之后会打开生成目录
        gc.setAuthor("lxl");//作者名字
        //配置父项目路径
        gc.setOutputDir(parent);
        //设置生成的service不带I
        gc.setServiceName("%sService");
        //把全局配置放入到生成器对象中
        mpg.setGlobalConfig(gc);
        //配置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUrl("jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf8");
        dsc.setUsername("root");
        dsc.setPassword("root");
        //把数据源对象放到生成器对象中
        mpg.setDataSource(dsc);

        //包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.powernode");
        //自定义生成路径
        Map<String,String> pathInfo = new HashMap<String, String>();
        pathInfo.put("entity_path",entityPath);
        pathInfo.put("mapper_path",parent + otherPath + "/src/main/java/com/powernode/mapper");
        pathInfo.put("xml_path",parent + otherPath + "/src/main/resources/com/powernode/mapper");
        pathInfo.put("service_path",parent + otherPath + "/src/main/java/com/powernode/service");
        pathInfo.put("service_impl_path",parent + otherPath + "/src/main/java/com/powernode/service/impl");
        pathInfo.put("controller_path", parent + otherPath + "/src/main/java/com/powernode/controller");
        pc.setPathInfo(pathInfo);
        //把包配置对象放到生成器对象中
        mpg.setPackageInfo(pc);

        //策略配置
        StrategyConfig sc = new StrategyConfig();
        //把表名的下划线转成驼峰命名法
        sc.setNaming(NamingStrategy.underline_to_camel);
        //把字段名下划线转驼峰
        sc.setColumnNaming(NamingStrategy.underline_to_camel);
        //设置entity的父类
        sc.setSuperEntityClass(BaseEntity.class);
        //设置不生成字段
        sc.setSuperEntityColumns("id");
        //设置entity 都加 @Data注解
        sc.setEntityLombokModel(true);
        //设置所有的controller都加@RestController注解
        sc.setRestControllerStyle(true);
        //设置Controller类中 父目录
        //sc.setControllerMappingHyphenStyle(true);
        //设置生成的表名
        sc.setInclude(tableName);
        //将策略配置对象放到生成器对象中
        mpg.setStrategy(sc);
        //设置模版引擎
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        //-------配置结束--------
        //生成代码
        mpg.execute();

    }
}
